package fr.mairie.parc.page.demande;

import java.util.List;

import javax.annotation.Resource;

import org.apache.click.Context;
import org.apache.click.control.ActionLink;
import org.apache.click.control.Column;
import org.apache.click.control.Decorator;
import org.apache.click.control.PageLink;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.TableInlinePaginator;
import org.apache.click.util.Bindable;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import fr.mairie.parc.context.AppContext;
import fr.mairie.parc.domain.Demande;
import fr.mairie.parc.page.BorderPage;
import fr.mairie.parc.service.DemandeManager;

@Component
public class DemandePage extends BorderPage{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

		@Resource protected DemandeManager demandeManager;
	 	@Bindable protected Table table = new Table("table");

	 	private Column actionColumn;
	    private ActionLink viewLink = new ActionLink("view", this, "onViewClick");
	    private PageLink editLink = new PageLink("edit",DemandePage.class);
	    private ActionLink deleteLink = new ActionLink("delete", this, "onDeleteClick");


	    public DemandePage() {

	        addControl(table);
	        addControl(viewLink);
	        addControl(editLink);
	        addControl(deleteLink);

	        table.setClass(Table.CLASS_ITS);
			table.setPageSize(10);
			table.setShowBanner(true);
			table.setSortable(true);
			table.setPaginator(new TableInlinePaginator(table));
			table.setPaginatorAttachment(Table.PAGINATOR_INLINE);

//	        Column column = new Column("IdDemande","Id demande");
//	        column.setSortable(false);
//	        table.addColumn(column);

	        Column column = new Column("numParc","Numéro parc");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("marque","Marque");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("modele","Modéle");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("date","Date");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("batiment","Batiment");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("etage","Etage");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("Salle");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("Direction");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("Service");
	        column.setAutolink(true);
	        table.addColumn(column);

	        column = new Column("Utilisateur");
	        column.setAutolink(true);
	        table.addColumn(column);

	        viewLink.setTitle("View customer details");

	        editLink.setListener(this, "onEditClick");
	        editLink.setTitle("Editer une attribution");
	        editLink.setParameter("onclick", "/demande/edit-demande.htm");

	        deleteLink.setTitle("Delete customer record");
	        deleteLink.setAttribute("onclick", "return window.confirm('Are you sure you want to delete this record?');");

	        column = new Column("Action");
	        column.setDecorator(new Decorator() {
	            public String render(Object row, Context context) {
	                Demande demande = (Demande) row;
	                String id = String.valueOf(demande.getIdDemande());

	                viewLink.setValue(id);
	                editLink.setParameter("id", id);
	                deleteLink.setValue(id);

	                return viewLink.toString() + " | " +
	                       editLink.toString() + " | " +
	                       deleteLink.toString();
	            }
	        });
	        table.addColumn(column);


	        table.setDataProvider(new DataProvider<Demande>() {
				private static final long serialVersionUID = 1L;
				public List<Demande> getData() {
	                return demandeManager.selectAll();
	            }
	        });


	    }

	    public void onInit(){
	    	super.onInit();
	    	ApplicationContext ctx = AppContext.getApplicationContext();
	    	demandeManager = (DemandeManager) ctx.getBean("demandeManager");
	    }

	    public boolean onViewClick() {
	        Integer id = viewLink.getValueInteger();
	        Demande detailDemande = demandeManager.getDemandeById(id);
	        addModel("detailDemande", detailDemande);
	        return true;
	    }

	    public boolean onDeleteClick() {
	        Integer id = deleteLink.getValueInteger();
	        //demandeDao.deleteCustomer(id);
	        return true;
	    }
}
